package net.huashitong.Project.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.Project.dao.EarlyWarningNodeDao;
import net.huashitong.Project.entity.EarlyWarningNode;
import net.huashitong.Project.filter.EarlyWarningNodeFilter;
import net.huashitong.user.filter.BnpUserFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class EarlyWarningNodeService extends StringPKService<EarlyWarningNode> {

    @Autowired
    private EarlyWarningNodeDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<EarlyWarningNode, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(EarlyWarningNode obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_early_warning_node", null));
    }

    public EarlyWarningNode queryEarlyWarningNodeByNodeSysId(String nodeSysId) {
        return dao.queryEarlyWarningNodeByNodeSysId(nodeSysId);
    }

    public List<EarlyWarningNode> queryEarlyWarningNodeListByStatus() {
        return dao.queryEarlyWarningNodeListByStatus();
    }

    public List<EarlyWarningNode> queryEarlyWarningNodeListByUserId(BnpUserFilter filter) {
        return dao.queryEarlyWarningNodeListByUserId(filter);
    }

    public List<EarlyWarningNode> queryEarlyWarning(EarlyWarningNodeFilter filter) {
        if (filter.getPage() == null) {
            filter.setPage(1);
        }
        Paging p = new Paging();
        p.setCurrentPage(filter.getPage());
        filter.setPaging(p);
        return dao.queryEarlyWarning(filter);
    }

    public Double querywarningCount(EarlyWarningNodeFilter filter) {
        double count = dao.querywarningCount(filter);
        Double page = Math.ceil(count / 10);

        return page;
    }

    public int querywarningCount1(EarlyWarningNodeFilter filter) {
        int count = dao.querywarningCount(filter);
        return count;
    }

    /**
     * 查询所有部门节点预警信息处理率
     */
    public Map<String, Object> queryEarlyWarningStateByDept() {
        String[] depts = {"自然资源局", "住建局", "发改局", "生态环境局", "水利局", "林业局", "财政局", "消防大队", "人防办", "人社局"};
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        int yezhuTotal = dao.queryEarlyWarningStateByDept(null, null);
        double yezhuComplete = (double) dao.queryEarlyWarningStateByDept(null, "1");
        if (yezhuTotal != 0) {
            map.put("业主单位", (yezhuComplete / yezhuTotal) * 100);
        } else {
            map.put("业主单位", 0);
        }
        for (String dept : depts) {
            int total = dao.queryEarlyWarningStateByDept(dept, null);
            double complete = (double) dao.queryEarlyWarningStateByDept(dept, "1");
            if (total != 0) {
                map.put(dept, (complete / total) * 100);
            } else {
                map.put(dept, 0);
            }
        }
        return map;
    }

    public List<EarlyWarningNode> queryEarlyWarningNodeList() {
        return dao.queryEarlyWarningNodeList();
    }

    /**
     * 获取所有预警未处理的信息
     *
     * @return
     */
    public List<EarlyWarningNode> getAllEarlyWarningNodeNoDealWith() {
        return dao.getAllEarlyWarningNodeNoDealWith();
    }

    public List<EarlyWarningNode> getEarlyWarningByProjectIdAndNodeId(String projectId, String nodeId) {
        return dao.getEarlyWarningByProjectIdAndNodeId(projectId,nodeId);
    }
}
